package group.cyfc.studygo.dto;

import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import group.cyfc.studygo.config.FastJsonEnumSerializer;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.experimental.Accessors;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;

import java.io.Serializable;

@ApiModel("返回对象")
@Data
@Slf4j
@Accessors(chain = true)
public class JsonResult<T> implements Serializable {

    @JsonSerialize(using = FastJsonEnumSerializer.class)
    @ApiModelProperty("响应码")
    private HttpStatus code;

    @ApiModelProperty("描述")
    private String message;

    @ApiModelProperty("数据")
    private T data;

    public JsonResult() {}

    public JsonResult(HttpStatus code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    public JsonResult result(HttpStatus code, String message, T data) {
        return new JsonResult(code, message, data);
    }

    public JsonResult ok() {
        return new JsonResult(HttpStatus.OK, "操作成功", null);
    }

    public JsonResult ok(String message) {
        return new JsonResult(HttpStatus.OK, message, null);
    }

    public JsonResult ok(T data) {
        return new JsonResult(HttpStatus.OK, "", data);
    }

    public JsonResult ok(String message, T data) {
        return new JsonResult(HttpStatus.OK, message, data);
    }

    public JsonResult serverError() {
        return new JsonResult(HttpStatus.INTERNAL_SERVER_ERROR, "服务器异常", null);
    }

    public JsonResult serverError(String message) {
        return new JsonResult(HttpStatus.INTERNAL_SERVER_ERROR, message, null);
    }

    public JsonResult serverError(T data) {
        return new JsonResult(HttpStatus.INTERNAL_SERVER_ERROR, "", data);
    }

    public JsonResult serverError(String message, T data) {
        return new JsonResult(HttpStatus.INTERNAL_SERVER_ERROR, message, data);
    }

    public JsonResult clientError() {
        return new JsonResult(HttpStatus.BAD_REQUEST, "数据错误", null);
    }

    public JsonResult clientError(String message) {
        return new JsonResult(HttpStatus.BAD_REQUEST, message, null);
    }

    public JsonResult clientError(T data) {
        return new JsonResult(HttpStatus.BAD_REQUEST, "", data);
    }

    public JsonResult clientError(String message, T data) {
        return new JsonResult(HttpStatus.BAD_REQUEST, message, data);
    }

    public JsonResult clientError(HttpStatus code,String message,T data){ return new JsonResult(code,message,data); }
}
